﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using GPSTracker.Helper;
using System.Data.SqlClient;
using System.Web.UI;

namespace GPSTracker.Views.Report
{
    public partial class TempoTrackingGPS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    GetGpsCords();
                }
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "Tempo TrackingGPS", "Page_Load");
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alertmessage", "javascript:alert('" + Helper.ExceptionHandler.GetErrorMsg(ex) + "');", true);
            }
        }

        public void GetGpsCords()
        {
            try
            {
                var strTempoID = string.Empty;
                var dtFromDate = System.DateTime.Now;
                var dtToDate = System.DateTime.Now;

                if (Session["TempoID"] != string.Empty && Session["TempoID"] != null)
                {
                    strTempoID = Session["TempoID"].ToString();
                }

                if (Session["FromDate"] != string.Empty && Session["FromDate"] != null)
                {
                    dtFromDate = Convert.ToDateTime(Session["FromDate"]);
                }

                if (Session["ToDate"] != string.Empty && Session["ToDate"] != null)
                {
                    dtToDate = Convert.ToDateTime(Session["ToDate"]);
                }

                var dt = new DataTable();
                var cmd = new SqlCommand("SP_GetGPSData", CommonFx.GetSqlCon());
                cmd.CommandType = CommandType.StoredProcedure;

                if (strTempoID != string.Empty)
                {
                    cmd.Parameters.AddWithValue("@pTempoID", strTempoID);
                }
                else
                {
                    cmd.Parameters.AddWithValue("@pTempoID", DBNull.Value);
                }

                cmd.Parameters.AddWithValue("@pFromDttm", dtFromDate);
                cmd.Parameters.AddWithValue("@pToDttm", dtToDate);

                var da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(dt);

                js.Text = this.PlotGPSPoints(dt);
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "Tempo TrackingGPS", "GetGpsCords");
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alertmessage", "javascript:alert('" + Helper.ExceptionHandler.GetErrorMsg(ex) + "');", true);
            }
        }

        public String PlotGPSPoints(DataTable tblPoints)
        {
            try
            {
                var Locations = string.Empty;
                var sJScript = string.Empty;
                var i = 0;

                foreach (DataRow r in tblPoints.Rows)
                {
                    if (r["latitude"].ToString().Trim().Length == 0)
                    {
                        continue;
                    }

                    var Latitude = r["latitude"].ToString();
                    var Longitude = r["longitude"].ToString();

                    Locations += Environment.NewLine + @"
                path.push(new google.maps.LatLng(" + Latitude + ", " + Longitude + @"));

                var marker" + i.ToString() + @" = new google.maps.Marker({
                    position: new google.maps.LatLng(" + Latitude + ", " + Longitude + @"),
                    title: '#' + path.getLength(),
                    map: map
                });";
                    i++;
                }

                sJScript = @"<script type='text/javascript'>

            var poly;
            var map;

            function initialize() {

                var cmloc = new google.maps.LatLng("+tblPoints.Rows[0]["Latitude"].ToString()+", "+tblPoints.Rows[0]["Longitude"].ToString() + @") 
                
                var myOptions = {
                    zoom: 13,
                    center: cmloc,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

                var polyOptions = {
                    strokeColor: 'blue',
                    strokeOpacity: 0.5,
                    strokeWeight: 3
                }
                poly = new google.maps.Polyline(polyOptions);
                poly.setMap(map);

                var path = poly.getPath();

               " + Locations + @"

                    }
                </script>";
                return sJScript;
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "Tempo TrackingGPS", "Page_Load");
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alertmessage", "javascript:alert('" + Helper.ExceptionHandler.GetErrorMsg(ex) + "');", true);
                return string.Empty;
            }
        }
    }
}
